package com.example.myapplication;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.widget.TextView;

import java.util.Calendar;


public class MyCalendar {
    private static final Calendar calendar = Calendar.getInstance();
    // 得到当前年月日
    private static final int CurrentYear = calendar.get(Calendar.YEAR);
    private static final int CurrentMonth = calendar.get(Calendar.MONTH);
    private static final int CurrentDay = calendar.get(Calendar.DAY_OF_MONTH);
    // 得到当前时间
    private static final int CurrentHour  = calendar.get(Calendar.HOUR_OF_DAY);
    private static final int CurrentMinute  = calendar.get(Calendar.MINUTE);


    /**
     *
     * @param dateTime  日期  要求格式yyyy-mm-dd
     * @return
     */
    private static int[] toInteger(String dateTime){
        int[] date=new int[3];
        String yearStr = dateTime.split("-")[0];
        String monthStr = dateTime.split("-")[1];
        String dayStr = dateTime.split("-")[2];

        int year = Integer.parseInt(yearStr);
        int month = Integer.parseInt(monthStr)-1;
        int day = Integer.parseInt(dayStr);

        return date;
    }

    /**
     *
     * @param context   上下文
     * @param dateView  展示日期控件
     */
    @SuppressLint("SetTextI18n")
    public static void getCalendar(Context context, TextView dateView){
        new DatePickerDialog(context, (view, year, month, dayOfMonth) -> {
            dateView.setText(year+"-"+(month+1)+"-"+dayOfMonth);
        },CurrentYear,CurrentMonth-1,CurrentDay).show();
    }

    /**
     *
     * @param context   上下文
     * @param dateView  展示日期控件
     * @param year      默认年份
     * @param month     默认月份
     * @param day       默认具体某天
     */
    @SuppressLint("SetTextI18n")
    public static void getCalendar(Context context, TextView dateView, int year, int month, int day){
        new DatePickerDialog(context, (view, year1, month1, dayOfMonth) -> {
            dateView.setText(year1 +"-"+ (month1+1) +"-"+dayOfMonth);
        },year,month,day).show();
    }

    /**
     *
     * @param context   上下文
     * @param dateView  展示日期控件
     * @param date      String类型的日期
     */
    @SuppressLint("SetTextI18n")
    public static void getCalendarStr(Context context, TextView dateView, String date){
        new DatePickerDialog(context, (view, year1, month1, dayOfMonth) -> {
            dateView.setText(year1 +"-"+ (month1+1) +"-"+dayOfMonth);
        },toInteger(date)[0],toInteger(date)[1]-1,toInteger(date)[2]).show();
    }

    /**
     *
     * @param context  上下文
     * @param timeView  展示时间的控件
     */
    @SuppressLint("SetTextI18n")
    public static void getTime(Context context,TextView timeView){
        new TimePickerDialog(context,(view, hourOfDay, minuteOfDay) -> {
            timeView.setText(hourOfDay+":"+minuteOfDay);
        }, CurrentHour, CurrentMinute, true).show();
    }

    /**
     *
     * @param context  上下文
     * @param timeView 展示时间的控件
     * @param hour     默认时间
     * @param minute   默认分钟
     */
    @SuppressLint("SetTextI18n")
    public static void getTime(Context context,TextView timeView, int hour, int minute){
        new TimePickerDialog(context, (view, hourOfDay, minuteDay) -> {
            timeView.setText(hourOfDay+":"+minuteDay);
        }, hour, minute, true).show();
    }

}

